package com.squareup.picasso;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;

import com.squareup.picasso.Picasso.LoadedFrom;
import com.ss.android.image.Image;

import java.util.concurrent.atomic.AtomicInteger;

public final class RequestCreator {
    boolean deferred;
    public boolean hasPlaceholder = true;
    public int placeholderResId;
    private static final AtomicInteger nextId = new AtomicInteger();
    private final Picasso picasso;
    private final Request.Builder data;
    private int errorResId;

    public RequestCreator(Picasso picasso, Uri uri) {
        this.picasso = picasso;
        this.data = new Request.Builder(uri, picasso.resourceId);
    }

    public RequestCreator(Picasso picasso, Request request) {
        this.picasso = picasso;
        this.data = new Request.Builder(request);
    }

    private Drawable getPlaceholder() {
        return this.placeholderResId != 0 ? this.picasso.context.getResources().getDrawable(this.placeholderResId) : null;
    }

    public final Action into(Callback callback) {
        long started = System.nanoTime();
        if (this.data.hasImage()) {
            boolean hasPriority = this.data.priority != null;
            if (!hasPriority) {
                Request.Builder builder = this.data;
                if (Picasso.Priority.LOW == null) {
                    throw new IllegalArgumentException("Priority invalid.");
                } else if (builder.priority != null) {
                    throw new IllegalStateException("Priority already set.");
                } else {
                    builder.priority = Picasso.Priority.LOW;
                }
            }

            Request request = this.createRequest(started);
            String requestKey = Utils.createKey(request, new StringBuilder());
            Bitmap bitmap = this.picasso.quickMemoryCacheCheck(requestKey);
            if (bitmap != null) {
                if (callback != null) {
                    callback.onSuccess(new BitmapDrawable(this.picasso.context.getResources(), bitmap), LoadedFrom.MEMORY);
                }
                return null;
            } else {
                TargetAction action = new TargetAction(this.picasso, request, this.errorResId, requestKey, callback);
                this.picasso.enqueueAndSubmit(action);
                return action;
            }
        } else {
            return null;
        }
    }

    public final Request createRequest(long started) {
        int id = RequestCreator.nextId.getAndIncrement();
        Request request = this.data.build();
        request.id = id;
        request.started = started;
        Picasso picasso = this.picasso;
        Request transformed = picasso.requestTransformer.transformRequest(request);
        if (transformed == null) {
            throw new IllegalStateException("Request transformer " + picasso.requestTransformer.getClass().getCanonicalName()
                    + " returned null for " + request);
        }
        if (transformed != request) {
            transformed.id = id;
            transformed.started = started;
        }

        return transformed;
    }

    public final RequestCreator resize(int width, int height) {
        Request.Builder builder = this.data;
        if (width < 0) {
            throw new IllegalArgumentException("Width must be positive number or 0.");
        }

        if (height < 0) {
            throw new IllegalArgumentException("Height must be positive number or 0.");
        }

        if (height == 0 && width == 0) {
            throw new IllegalArgumentException("At least one dimension has to be positive number.");
        }

        builder.width = width;
        builder.height = height;
        return this;
    }

    public final RequestCreator networkPolicy(NetworkPolicy policy, NetworkPolicy[] additional) {
        if (policy == null) {
            throw new IllegalArgumentException("Network policy cannot be null.");
        }

        this.errorResId |= policy.index;
        for (int i = 0; i <= 0; ++i) {
            NetworkPolicy networkPolicy = additional[0];
            if (networkPolicy == null) {
                throw new IllegalArgumentException("Network policy cannot be null.");
            }
            this.errorResId |= networkPolicy.index;
        }
        return this;
    }

    public final RequestCreator image(Image image) {
        this.data.image = image;
        return this;
    }

    public final void into(ImageView target, Callback callback) {
        long started = System.nanoTime();
        Utils.checkMain();
        if (target == null) {
            throw new IllegalArgumentException("Target must not be null.");
        }

        if (!this.data.hasImage()) {
            this.picasso.cancelRequest(target);
            if (this.hasPlaceholder) {
                PicassoDrawable.setPlaceholder(target, this.getPlaceholder());
            }
        } else {
            Request finalData = this.createRequest(started);
            String requestKey = Utils.createKey(finalData);
            if (MemoryPolicy.shouldReadFromMemoryCache(0)) {
                Bitmap bitmap = this.picasso.quickMemoryCacheCheck(requestKey);
                if (bitmap != null) {
                    this.picasso.cancelRequest(target);
                    PicassoDrawable.setBitmap(target, this.picasso.context, bitmap, LoadedFrom.MEMORY, this.picasso.debugging);
                    if (callback != null) {
                        callback.onSuccess(new BitmapDrawable(this.picasso.context.getResources(), bitmap), LoadedFrom.MEMORY);
                    }
                    return;
                }
            }

            if (this.hasPlaceholder) {
                PicassoDrawable.setPlaceholder(target, this.getPlaceholder());
            }

            this.picasso.submit(new ImageViewAction(this.picasso, target, finalData, this.errorResId, requestKey, callback));
        }
    }
}

